package com.ruoyi.dynamic.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dynamic.domain.XlkjDynamic;
import com.ruoyi.dynamic.service.IXlkjDynamicService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作动态Controller
 *
 * @author wang
 * @date 2022-01-13
 */
@RestController
@RequestMapping("/h5/dynamic/dynamic")
public class XlkjDynamicControllerH5 extends BaseController
{
    @Autowired
    private IXlkjDynamicService xlkjDynamicService;

    /**
     * 查询工作动态列表
     */
//    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjDynamic xlkjDynamic)
    {
        startPage();
        List<XlkjDynamic> list = xlkjDynamicService.selectXlkjDynamicList(xlkjDynamic);
        for(XlkjDynamic dynamic:list){
            //图片路径拼接
            dynamic.setImg(UrlUtils.getUrl() + dynamic.getImg());
        }
        return getDataTable(list);
    }

    /**
     * 导出工作动态列表
     */
//    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:export')")
//    @Log(title = "工作动态", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjDynamic xlkjDynamic)
    {
        List<XlkjDynamic> list = xlkjDynamicService.selectXlkjDynamicList(xlkjDynamic);
        ExcelUtil<XlkjDynamic> util = new ExcelUtil<XlkjDynamic>(XlkjDynamic.class);
        return util.exportExcel(list, "工作动态数据");
    }

    /**
     * 获取工作动态详细信息
     */
//    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        XlkjDynamic dynamic = xlkjDynamicService.selectXlkjDynamicById(id);
        //内容图片处理
        String url = dynamic.getContent().replaceAll("/prod-api", UrlUtils.getUrl());
//        String url = dynamic.getContent().replaceAll("//121.89.226.89:8083", UrlUtils.getUrl());
        //修改图片溢出问题
        String urlTo = url.replaceAll("<img", "<img style='max-width:100%;height:auto;'");
        dynamic.setContent(urlTo);
        return AjaxResult.success(dynamic);
    }

    /**
     * 新增工作动态
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:add')")
    @Log(title = "工作动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjDynamic xlkjDynamic)
    {
        return toAjax(xlkjDynamicService.insertXlkjDynamic(xlkjDynamic));
    }

    /**
     * 修改工作动态
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:edit')")
    @Log(title = "工作动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjDynamic xlkjDynamic)
    {
        return toAjax(xlkjDynamicService.updateXlkjDynamic(xlkjDynamic));
    }

    /**
     * 删除工作动态
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamic:remove')")
    @Log(title = "工作动态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjDynamicService.deleteXlkjDynamicByIds(ids));
    }
}
